{extend name="admin/base" /}
{block name="resources"/}
<script src="ADMIN_JS/My97DatePicker/WdatePicker.js" type="text/javascript"></script>
<style  type="text/css">
.set-style{
	width:100%;
}
.set-style dl{
	width:100%;
	font-size: 0;
	/* line-height: 20px; */
	clear: both;
	padding: 0;
	margin: 0;
	border-bottom: dotted 1px #E6E6E6;
	overflow: hidden;
}
.set-style dl dt{
	font-size: 12px;
	line-height: 32px;
	vertical-align: top;
	letter-spacing: normal;
	word-spacing: normal;
	text-align: right;
	display: inline-block;
	width: 19%;
	padding: 10px 1% 10px 0;
	margin: 0;
	font-weight:normal;
}
.set-style dl dd{
	
	font-size: 12px;
	line-height: 32px;
	vertical-align: top;
	letter-spacing: normal;
	word-spacing: normal;
	display: inline-block;
	width: 78%;
	padding: 10px 0 10px 0;
}
.set-style dl dd input[type="text"]{
	font: 12px/20px Arial;
	color: #777;
	background-color: #FFF;
	vertical-align: top;
	display: inline-block;
	height:24px;
	line-height:24px;
	padding: 4px;
	border: 1px solid #CCC;
	outline: 0 none;
	width: 400px;
	margin:0;
}
.set-style dl dd textarea{
	font: 12px/20px Arial;
	color: #777;
	background-color: #FFF;
	vertical-align: top;
	display: inline-block;
	height: 60px;
	padding: 4px;
	border: solid 1px #CCC;
	outline: 0 none;
	width: 400px;
	margin:0;
}
.hint{
	font-size: 12px;
	line-height: 16px;
	color: #BBB;
	margin-top: 10px;
}
.set-style dl dd p img{
	border: medium none;
	vertical-align: middle;
}
.class-logo{
	background-color: #FFF;
	position: relative;
	z-index: 1;
}
.class-logo p{
	width: 120px;
	height: 100px;
	line-height: 0;
	background-color: #FFF;
	text-align: center;
	vertical-align: middle;
	display: table-cell;
	*display: block;
	overflow: hidden;
	margin: 0 0 10px;
	border: dashed 1px #E6E6E6;
}
.class-logo p img{
	max-width: 120px;
	max-height: 100px;
	border: medium none;
	vertical-align: middle;
	margin-top: expression(60-this.height/2);
	*margin-top: expression(30-this.height/2);
}

.set-style dl dd p{
	margin-bottom:0;
}
</style>
 {/block}
{block name="main"}
<div class="set-style">
	<h2>活动信息</h2>
	<dl>
		<dt><span style="color:red;">*</span>&nbsp;&nbsp;活动名称:</dt>
		<dd>
			<input type="text" id="mansong_name" maxlength="10"/>
			<p class="error">请输入活动名称</p>
		</dd>
	</dl>
	<dl>
		<dt><span style="color:red;">*</span>&nbsp;&nbsp;生效时间:</dt>
		<dd>
			<input class="input-medium" type="text" id="start_time" style="width:250px;"onclick="WdatePicker({skin:'twoer',dateFmt:'yyyy-MM-dd HH:mm:ss'})" />
			<span class="mlr5">至</span>
			<input class="input-medium" size="15"type="text" id="end_time" style="width:250px;"onclick="WdatePicker({skin:'twoer',dateFmt:'yyyy-MM-dd HH:mm:ss'})">
			<p class="error">请输入生效时间</p>
		</dd>
	</dl>
	
	<h2>优惠设置</h2>
	<dl>
		<dt>优惠方式:</dt>
		<dd>
			<label><input class="input-mini" type="radio" name="type" value="1" checked="checked" /> 普通优惠</label>
			<label><input class="input-mini" type="radio" name="type" value="2" /> 多级优惠</label>
			<p class="hint">（每级优惠不累积叠加）</p>
		</dd>
	</dl>
	<dl>
		<dt><span style="color:red;">*</span>&nbsp;&nbsp;优惠条件:</dt>
		<dd style="width:100%;">
			<div class="table-responsive">
				<table class="table">
					<thead>
						<tr>
							<th>层级</th>
							<th>优惠门槛</th>
							<th>优惠方式（可多选）</th>
							<th>操作</th>
						</tr>
					</thead>
					<tbody>
						<tr>
							<td>1</td>
							<td>满<input type="text" id="price1" value="" style="width:50px;">元</td>
							<td>
								<div>
									<p>
										<label>
											<input type="checkbox" name="mansong" class="discount1" onchange="set_rule('discount',1,this)" />
											<span id="discount1">减现金</span>
										</label>
									</p>
									<p>
										<label>
											<input type="checkbox" name="mansong" class="free_shipping1" />
											<span id="free_shipping1">免邮</span>
										</label>
									</p>
									<p>
										<label>
											<input type="checkbox" name="mansong" class="give_point1" onchange="set_rule('give_point',1,this)" />
											<span id="give_point1">送积分</span>
										</label>
									</p>
									<p>
										<label>
											<input type="checkbox" name="mansong" class="give_coupon1" onchange="set_rule('give_coupon',1,this)" />
											<span id="give_coupon1">送优惠券</span>
										</label>
									</p>
									<p>
										<label>
											<input type="checkbox" name="mansong" class="gift_id1" onchange="set_rule('gift_id',1,this)" />
											<span id="gift_id1">送赠品</span>
										</label>
									</p>
								</div>
							</td>
							<td></td>
						</tr>
					</tbody>
					<tfoot style="display:none;">
						<tr>
							<td colspan="4">
								<a href="javascript:void(0)" onclick="add_reward()">+新增一级优惠</a>
								<span class="gray pl20">最多可设置五个层级</span>
							</td>
						</tr>
					</tfoot>
				</table>
			</div>
		</dd>
	</dl>
	
	<h2>选择活动商品</h2>
	<dl>
		{include file="admin/Promotion/selectGoods" /}
		<button class="edit_button" onclick="addMansong();">提交</button>
	</dl>
</div>

<script>
$(function(){
	LoadingInfo(1);
});
</script>
<script type="text/javascript">
var flag = false;//防止重复提交
//添加一级优惠
	function add_reward(){
		var len = $(".table tbody tr").length;
		if(len >= 5){
			$(".table tfoot").hide();
		}else{
			var cengji =  Number(len)+1;
			var html = '';
			html += '<tr>';
			html += '<td>'+cengji+'</td>';
			html += '<td>满<input type="text" id="price'+cengji+'" value="" style="width:50px;">元</td>';
			html += '<td><div>';
			html += '<p><label><input type="checkbox" name="mansong" class="discount'+cengji+'" onchange="set_rule(\'discount\','+cengji+',this)"><span id="discount'+cengji+'">减现金</span></label></p>';
			html += '<p><label><input type="checkbox" name="mansong" class="free_shipping'+cengji+'"><span id="free_shipping'+cengji+'">免邮</span></label></p>';
			html += '<p><label><input type="checkbox" name="mansong" class="give_point'+cengji+'" onchange="set_rule(\'give_point\','+cengji+',this)"><span id="give_point'+cengji+'">送积分</span></label></p>';
			html += '<p><label><input type="checkbox" name="mansong" class="give_coupon'+cengji+'" onchange="set_rule(\'give_coupon\','+cengji+',this)"><span id="give_coupon'+cengji+'">送优惠券</span></label></p>';
			html += '<p><label><input type="checkbox" name="mansong" class="gift_id'+cengji+'" onchange="set_rule(\'gift_id\','+cengji+',this)"><span id="gift_id'+cengji+'">送赠品</span></label></p>';
			html += '</div></td>';
			html += '<td><a href="javascript:void(0);" onclick="del_cengji('+cengji+')">删除</a></td>';
			html += '</tr>';
			$(".table tbody").append(html);
			if(cengji == 5){
				$(".table tfoot").hide();
			}
		}
	}
	
	function del_cengji(cengji){
		$(".table tbody tr:last").remove();
	}
	
	$("input[name='type']").change(function(){
		var c = $(this).val();
		if(c == 2){
			$(".table tfoot").show();
		}else{
			$(".table tbody tr:gt(0)").remove();
			$(".table tfoot").hide();
		}
	});
	
	//优惠类型点击
	function set_rule(type,num,e){
		if(type == 'discount'){
			discount(num,e);
		}else if(type == 'give_point'){
			give_point(num,e);
		}else if(type == 'give_coupon'){
			give_coupon(num,e);
		}else if(type == 'gift_id'){
			gift_id(num,e);
		}
	}
	
	//减现金
	function discount(num,e){
		if(e.checked == true){
			var html = "减<input type='text' id='discount_input"+num+"' style='width:50px;'/>元";
		}else{
			var html = "减现金";
		}
		$("#discount"+num).html(html);
	}
	
	//送积分
	function give_point(num,e){
		if(e.checked == true){
			var html = "送<input type='text' id='give_point_input"+num+"' style='width:50px;'/>积分";
		}else{
			var html = "送积分";
		}
		$("#give_point"+num).html(html);
	}
	
	//送优惠券
	function give_coupon(num,e){
		if(e.checked == true){
			$.ajax({
				type : "post",
				url : "{:__URL('ADMIN_MAIN/promotion/coupontypelist')}",
				success : function(data) {
					var html = '送<select id="give_coupon_select'+num+'">';
					for(var i=0;i<data['data'].length;i++){
						html += '<option value="'+data['data'][i]['coupon_type_id']+'">'+data['data'][i]['coupon_name']+'</option>';
					}
					html += "</select>";
					$("#give_coupon"+num).html(html);
				}
			});
		}else{
			var html = "送优惠券";
			$("#give_coupon"+num).html(html);
		}
	}

	//送赠品
	function gift_id(num,e){
		if(e.checked == true){
			$.ajax({
				type : "post",
				url : "{:__URL('ADMIN_MAIN/promotion/giftlist')}",
				success : function(data) {
					var html = '送<select id="gift_id_select'+num+'">';
					for(var i=0;i<data['data'].length;i++){
						html += '<option value="'+data['data'][i]['gift_id']+'">'+data['data'][i]['gift_name']+'</option>';
					}
					html += "</select>";
					$("#gift_id"+num).html(html);
				}
			});
		}else{
			var html = "送赠品";
			$("#gift_id"+num).html(html);
		}
	}
	
	//保存
	function addMansong(){
		var mansong_name = $("#mansong_name").val();
		var start_time = $("#start_time").val();
		var end_time = $("#end_time").val();
		var type = $("input[type='radio'][name='type']:checked").val();
		var range_type = $("input[type='radio'][name='range_type']:checked").val();
		var rule = '';
		var obj = $(".table tbody tr");
		if(!verify(mansong_name, start_time, end_time)){
			return false;
		}
		for(var i=1;i<=obj.length;i++){
			//满减送价格
			if($("#price"+i).val() > 0){
				var price = $("#price"+i).val();
			}else{
				var price = 0;
				showMessage('error', '请输入优惠门槛金额');
				return false;
			}
			//减现金
			if($("input.discount"+i+"[type='checkbox']").is(':checked') == true){
				var discount = $("#discount_input"+i+"").val();
			}else{
				var discount = 0;
			}
			//免邮
			if($("input.free_shipping"+i+"[type='checkbox']").is(':checked') == true){
				var free_shipping = 1;
			}else{
				var free_shipping = 0;
			}
			//送积分
			if($("input.give_point"+i+"[type='checkbox']").is(':checked') == true){
				var give_point = $("#give_point_input"+i).val();
			}else{
				var give_point = 0;
			}
			//送优惠券
			if($("input.give_coupon"+i+"[type='checkbox']").is(':checked') == true){
				var give_coupon = $("#give_coupon_select"+i).val();
			}else{
				var give_coupon = 0;
			}
			//送赠品
			if($("input.gift_id"+i+"[type='checkbox']").is(':checked') == true){
				var gift_id = $("#gift_id_select"+i).val();
			}else{
				var gift_id = 0;
			}
			if(discount+free_shipping+give_point+give_coupon+gift_id == 0){
				showMessage('error', '请至少选择一种优惠方式');
				return false;
			}
			rule += ';'+price+','+discount+','+free_shipping+','+give_point+','+give_coupon+','+gift_id;
		}
		rule = rule.substring(1);
		var obj = $(".select-two table tbody tr");
		var goods_id_array = '';
		obj.each(function(i){
			goods_id_array += ','+obj.eq(i).attr("goodsid");
		});
		goods_id_array = goods_id_array.substring(1);
		if(goods_id_array == '' && range_type == 0){
			showMessage('error', '请选择参加活动的商品');
			return false;
		}
		if(flag){
			return;
		}
		flag = true;
		$.ajax({
			type : "post",
			url : "{:__URL('ADMIN_MAIN/promotion/addmansong')}",
			data : {
				'mansong_name' : mansong_name,
				'start_time' : start_time,
				'end_time' : end_time,
				'type' : type,
				'range_type' : range_type,
				'rule' : rule,
				'goods_id_array' : goods_id_array
			},
			success : function(data) {
				if (data["code"] > 0) {
					showMessage('success', data["message"], "{:__URL('ADMIN_MAIN/promotion/mansonglist')}");
				}else{
					showMessage('error', data["message"]);
					flag = false;
				}
			}
		});
	}
	
	/**
	* 模块输入信息验证
	*/
	function verify(mansong_name, start_time, end_time){
		if(mansong_name == ''){
			$("#mansong_name").parent().find('.error').show();
			return false;
		}else{
			$(".error").hide();
		}
		if(start_time == '' || end_time == ''){
			$("#start_time").parent().find('.error').show();
			return false;
		}else{
			$(".error").hide();
		}
		return true;
	}
</script>
{/block}